package com.aistar.service.job.impl;

import com.aistar.mapper.EmployeeCollectionMapper;
import com.aistar.mapper.RecruitmentMapper;
import com.aistar.model.entity.Employee;
import com.aistar.model.entity.EmployeeCollection;
import com.aistar.service.job.IEmployeeCollectionService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.HashOperations;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.data.redis.core.ValueOperations;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.concurrent.TimeUnit;

@Service
@Transactional
public class EmployeeCollectionServiceImpl implements IEmployeeCollectionService {
    @Autowired
    private EmployeeCollectionMapper employeeCollectionMapper;
    @Autowired
    private RedisTemplate redisTemplate;
    @Autowired
    private RecruitmentMapper recruitmentMapper;

    @Override
    public int addRecruitment(EmployeeCollection employeeCollection,Integer id,Integer eid) {
        List<Integer> employeeList=employeeCollectionMapper.findByEmployeeId(eid);
        if(!employeeList.contains(id)){
            return employeeCollectionMapper.save(employeeCollection);
        } else {
            return 0;
        }
//        if (employeeCollection.getEmployeeId()==eid && employeeCollection.getRecruitmentId()!=id){
//            employeeCollectionMapper.save(employeeCollection);
////            System.out.println(employeeCollection.getEmployeeId());
//        }else{
//            System.out.println("抱歉");
//        }
    }

    @Override
    public EmployeeCollection searchByRecruitmentId(Integer rid, Integer eid) {
        return employeeCollectionMapper.findByRecruitmentId(rid, eid);
    }

    @Override
    public int searchCount(Integer rid, Integer eid) {
        return employeeCollectionMapper.findByCountRecruitmentId(rid, eid);
    }

    @Override
    public int dropId(Integer rid, Integer eid) {
        int count = employeeCollectionMapper.delByRidAndEid(rid, eid);
        return count;

    }

    @Override
    public boolean judgeCollection(Integer employeeId, Integer recruitmentId) {
        //判断redis内是否存放了数据
        HashOperations vop=redisTemplate.opsForHash();
        String keys="employeeCollection_"+employeeId+"_"+recruitmentId;
        if(redisTemplate.hasKey(keys)){
            String employeeId1= (String) vop.get(keys,"employeeId".toString());
            String recruitmentId2= (String) vop.get(keys,"recruitmentId".toString());
            String recruitmentName= (String) vop.get(keys,"recruitmentName".toString());
            return true;
        }else {
            // 根据求职者id和职位id判断该求职者是否收藏该职位
            Integer count = employeeCollectionMapper.findByCountRecruitmentId(recruitmentId, employeeId);
            if (count == 0) {
                // count=0表示该用户没有收藏该职位
                return false;
            }
            return true;
        }
    }
    //保存到redis
    @Override
    public void saveToRedis(Integer id, Integer eid) {
        HashOperations vop = redisTemplate.opsForHash();
        String key="employeeCollection_"+eid+"_"+id;
        EmployeeCollection employeeCollection = new EmployeeCollection();
        employeeCollection.setEmployeeId(eid);
        employeeCollection.setRecruitmentName(recruitmentMapper.findByName(id));
        employeeCollection.setRecruitmentId(id);
        Map<String,String> maps=new HashMap<>();
        maps.put("employeeId",employeeCollection.getEmployeeId().toString());
        maps.put("recruitmentId",employeeCollection.getRecruitmentId().toString());
        maps.put("recruitmentName",employeeCollection.getRecruitmentName());
        vop.putAll(key,maps);
        redisTemplate.expire(key,3, TimeUnit.HOURS);
    }

    @Override
    public void saveToMySql(Integer id, Integer eid) {
        EmployeeCollection employeeCollection=new EmployeeCollection();
        employeeCollection.setRecruitmentId(id);
        employeeCollection.setRecruitmentName(recruitmentMapper.findByName(id));
        employeeCollection.setEmployeeId(eid);
        employeeCollectionMapper.save(employeeCollection);
    }

    public void delToRedis(Integer id,Integer eid){
        HashOperations vop = redisTemplate.opsForHash();
        String key="employeeCollection_"+eid+"_"+id;
        redisTemplate.delete(key);
    }

//    //判断是否收藏
//    @Override
//    public boolean makeCollection(Integer id, Integer eid) {
//        //判断redis内是否存放了数据
//        ValueOperations vop=redisTemplate.opsForValue();
//        String keys="employeeCollection_"+eid+"_"+id;
//        if(redisTemplate.hasKey(keys)){
//            String collection= (String) vop.get(keys);
//            return true;
//        }else {
//            //如果redis内没有记录,那么就去mysql中去查
//            //如果收藏表里存放了数据
//            boolean flag=judgeCollection(eid,id);
//            if (!flag){
//                String value=recruitmentMapper.findByName(id);
//                vop.set(keys,value);
//                //设置过期时间
//                redisTemplate.expire(keys,7,TimeUnit.DAYS);
//            }else{
//
//            }
//            return false;
//        }
//    }


}
